<?php
/**
 * Class descript.
 * User: chan
 * Date: 2017/10/26 15:30
 */
namespace app\api\logic;
use app\api\BaseModel;
use think\Cache;
use think\Db;
use think\Request;
use app\api\validate\Login as LoginValidate;

class Login extends BaseModel
{
    /*
     *	用户登陆
     */
    public function login()
    {
        (new LoginValidate())->checkLoginParam();
        $root = db('member')->where(['wx_openid' => $this->requestData['openid']])->find();
        if (!$root) {
            return array('message' => '账号不存在');
        } else {
            if ($root['status'] == 0) return array('message' => '账号禁止登录中');
            $Oauth2 = new \app\api\service\Oauth2();
            $user = [
                'userId' => $root['id'],
                'mobile' => $root['mobile'],
                'nickname' => $root['nickname'],
                'id' => $root['id'],
                'uid' => $root['id'],
                'avatar' => $root['headimage'],
                'sex' => $root['sex'] ? $root['sex'] : '',
                'headimage' => $root['headimage'],
                'wx_openid' => $root['wx_openid'],
            ];
            $tokenInfo = $Oauth2::generateToken($user);
            return $tokenInfo;
        }
    }


	/*
     *	用户注册
     */
    public function register()
    {
        (new LoginValidate())->checkRegisterParam();
		$code = $this->requestData['code'];
		$a = '/^1[34578]{1}\d{9}$/';
			if(empty(preg_match($a,$this->requestData['mobile']))){
				return array('message'=>'手机号码有误');
			}
        if (base64_encode($code) != Cache::get('register'.$this->requestData['mobile'])) {
            return array('message'=>'验证码错误');
        }else{
            $where['mobile'] = $this->requestData['mobile'];
            $whereor['wx_openid'] = $this->requestData['openid'];
		
            $root = db('member')->where($where)->whereor($whereor)->find();
            if($root){
                return array('message'=>'手机号码已注册或该微信已注册');
            }else{
                $data['mobile'] = $this->requestData['mobile'];
                $data['sex'] = intval($this->requestData['sex']);
                $data['nickname'] = array_key_exists('nickname',$this->requestData) !== false ? addslashes($this->requestData['nickname']) : '';
                $data['wx_openid'] = $this->requestData['openid'];
                $data['province'] = isset($this->requestData['province'])?$this->requestData['province']:'';
                $data['city'] = isset($this->requestData['city'])?$this->requestData['city']:'';
                $data['county'] = isset($this->requestData['country'])?$this->requestData['country']:'';
                $data['headimage'] = $this->requestData['avatarUrl'];
                $data['register_time'] = time();

                
                if(db('member')->insert($data) === false) return array('message' => '注册失败');

                return array();
            }
        }
    }

	/*
     *	换手机号
     */
    public function bind()
    {
    	$uid      = intval($this->requestData['uid']);
		$member = checkMember($uid);
		if(!$member) return array('message'=>'此会员不存在');
        $mobile   = trim($this->requestData['mobile']);
		$code     = trim($this->requestData['code']);
		if(isset($this->requestData['passwd'])){
			$pass = trim($this->requestData['passwd']);
		}else{
			$pass = '';
		}

        if (base64_encode($code) != Cache::get('bind')) {
            return array('message'=>'验证码错误');
        }else{
            if(!empty($pass)){
            	$password = md5($pass);
				$word = $member['passwd'];
				if($password!=$word) return array('message'=>'原密码不正确');
			}
			$data['mobile'] = $mobile;
            $member_result = db('member')->where(array('mobile'=>$mobile))->field('id')->find();
            if($member_result['id'] != $uid)return array('message'=>'该修改号码已被注册过');

			$update = db('member')->where("id='$uid'")->update($data);
			if(!$update) return array('message'=>'更改失败');
			return array();
        } 
    }
	
}